package org.example.main.service.impl;

import org.apache.log4j.Logger;
import org.example.main.dao.FavoriteMapper;
import org.example.main.entity.Favorite;
import org.example.main.service.FavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FavoriteServiceImpl implements FavoriteService {

    private static final Logger logger = Logger.getLogger(FavoriteServiceImpl.class);

    @Autowired
    private FavoriteMapper favoriteMapper;

    @Override
    public int add(Favorite favorite) {
        logger.info("Adding favorite for tenant ID: " + favorite.getTenantId() + " and house ID: " + favorite.getHouseId());
        int result = favoriteMapper.insert(favorite);
        logger.info("Favorite added with ID: " + favorite.getFavoriteId() + ", result: " + result);
        return result;
    }

    @Override
    public int removeById(long favoriteId) {
        logger.info("Removing favorite by ID: " + favoriteId);
        int result = favoriteMapper.deleteById(favoriteId);
        logger.info("Favorite removal result: " + result);
        return result;
    }

    @Override
    public int removeByTenantAndHouse(long tenantId, long houseId) {
        logger.info("Removing favorite by tenant ID: " + tenantId + " and house ID: " + houseId);
        int result = favoriteMapper.deleteByTenantAndHouse(tenantId, houseId);
        logger.info("Favorite removal result: " + result);
        return result;
    }

    @Override
    public List<Favorite> getByTenantId(long tenantId) {
        logger.info("Getting favorites by tenant ID: " + tenantId);
        List<Favorite> favorites = favoriteMapper.findByTenantId(tenantId);
        logger.debug("Found " + favorites.size() + " favorites for tenant ID: " + tenantId);
        return favorites;
    }

    @Override
    public List<Favorite> getByHouseId(long houseId){
        logger.info("Getting favorites by house ID: " + houseId);
        List<Favorite> favorites = favoriteMapper.findByHouseId(houseId);
        logger.debug("Found " + favorites.size() + " favorites for house ID: " + houseId);
        return favorites;
    }

    @Override
    public Favorite getByTenantAndHouse(long tenantId, long houseId) {
        logger.info("Getting favorite by tenant ID: " + tenantId + " and house ID: " + houseId);
        Favorite favorite = favoriteMapper.findByTenantAndHouse(tenantId, houseId);
        logger.debug("Found favorite: " + favorite);
        return favorite;
    }

    @Override
    public List<Favorite> findAll(){
        logger.info("Retrieving all favorites");
        List<Favorite> favorites = favoriteMapper.findAll();
        logger.debug("Found " + favorites.size() + " favorites");
        return favorites;
    }
}
